%set_dynare_seed(42);
var
Ld_e,d_e, e, PH, CH,CF, L,O, PHstar,CHstar,
Fstar,LO, Phi,Pi, Bstar,
A,Nstar, PFstar, Rstar,POstar,R,PH_num, X1,X2,PI,rer, Y,

Yhat, ehat,qhat, CHhat,rerhat,Rhat,Lhat,LOhat,Pihat,CFhat,CHstarhat,Rstarhat,Bstarhat,Nstarhat,

v_noFXI, u_noFXI, 

CH_til,LO_til,O_til,PHstar_til,L_til,Delta_lnq2,

Delta_lnq,Delta_lnrer,v_peg,Pi_peg,O_peg,LO_peg,L_peg,CH_peg,

v_pegsub, Pi_pegsub,PHstar_pegsub,LO_pegsub,O_pegsub,CH_pegsub,

rer_dcp,CH_dcp,CF_dcp,LO_dcp,
O_dcp,Phi_dcp,Bstar_dcp,R_dcp,PH_dcp,PH_num_dcp,X1_dcp,
X2_dcp,PI_dcp,e_dcp,Ld_e_dcp,L_dcp, Pi_dcp,v_noFXI_dcp,u_noFXI_dcp,Delta_lnrer_dcp ,Fstar_dcp;


varexo 
epsn,epspf,epsa,epspos,epsrs; 

%the order for these params should match the matrix x.
parameters 
Cstar,Ow, gamma, alpha, epsx, beta, omega ,
volat,volat_dcp,epsh, theta, Rss,rhor,eta,rhon,rhoa,rhopos,rhors,rhopf,
Rstarb,gammaPi,gammaYY,Piss, LOss,CFss, Yss,PHstarss,CHstarss,ess,CHss,Lss,rerss,Oss,sigman;

x         = load('steadystate_OEXRPolicy.mat');
M_.params = cell2mat( struct2cell(x.paramsnew) );


%-------------------- Model equations----------------
model;

%------------------------------------------------------------------
%----------------------Sticky Price Model--------------------------
%------------------------------------------------------------------
%e
%e * PFstar/PH = gamma/(1-gamma) *CH/CF;
rer  = gamma/(1-gamma) *CH/CF;

%O
A * LO^alpha*O = CHstar + Phi*CH;

%Bstar
Bstar/Rstar - Bstar(-1) = PHstar*CHstar - PFstar*CF + POstar*(Ow - O);
%Bstar/Rstar - Bstar(-1) = PHstarss*CHstar - PFstar*CFss + POstar*(Ow - Oss);

%CF
%log(Rstar/Rss) + log(PFstar/PFstar(+1)) + log(CF/CF(+1))  = Rss *( Bstar - Nstar - Fstar  )/Yss * omega*volat*Yss; % omega*volat*Yss;
log(Rstar) -  log(R) + log(e(+1)/e)  = Rss *( Bstar - Nstar - Fstar  )/Yss * omega*volat*Yss;
% log(Rstar) -  log(R) +log(e(+1)/e)  =  ( 1*Bstar-Nstar  )* 0.06;

 
%LO
PFstar * CF = gamma* (alpha)/(1-alpha)* POstar/(LO);

%PHstar
PHstar = epsx/(epsx-1) * POstar/( (1-alpha)* A * LO^alpha   ); 
CHstar = PHstar^(-epsx)*Cstar;


%L
LO = L/O; 

%CH
1/R = beta* PH*CH/ ( PH(+1)*CH(+1) );



%-----------------Calvo Pricing--------------------
%PH
PH^(1-epsh) = (1-theta)*PH_num^(1-epsh) + theta*PH(-1)^(1-epsh);

%Phi
Phi = (1-theta)* (PH_num/PH)^(-epsh) + theta*Phi(-1);

%PH_num
PH_num = eta*epsh/(epsh-1)* X1/X2;

%X1
X1  =PI*PH^(epsh-1) +    beta*theta*X1(+1);

%X2
X2  =PH^(epsh-1)    +    beta*theta*X2(+1);

%PI
e*POstar = (1-alpha)*PI*A *LO^alpha;


%-------Policy-------------
%R
R/Rss      = (R(-1)/Rss)^rhor * (Pi/Piss)^(gammaPi*(1-rhor));

%Pi 
Pi = PH/PH(-1); 

Fstar        = 0;
%Fstar        = 0.99* (Bstar-Nstar);

%----------Exogenous Variables-----------------
Nstar/Yss            = rhon  * Nstar(-1)/Yss         + epsn;
log(PFstar)          = rhopf * log(PFstar(-1))       + epspf;
log(A)               = rhoa  * log(A(-1))            + epsa;
log(POstar)          = rhopos* log(POstar(-1))       + epspos;
log(Rstar/Rstarb)    = rhors * log(Rstar(-1)/Rstarb) + epsrs;

%-------------Others----------
rer = PFstar*e/PH  ;
Y   = A * LO^alpha*O;

Ld_e = 100*log(e(+1)/e); 
d_e =  e/e(+1);


%%%%%%%--------------------Percent Deviations---------------------

Yhat    = log(Y) - log(Yss); 
ehat    = log(e) - log(ess); 
qhat    = log(A)  - (1-alpha)* log(POstar);
CHhat   = log(CH)  -log(CHss);
rerhat  = log(rer) - log(rerss);
Rhat    = 4*(log(R) - log(Rss));
Lhat    = log(L)  - log(Lss);
LOhat   = log(LO) - log(LOss);
Pihat   = log(Pi) - log(Piss);
CFhat   = log(CF) - log(CFss);
Bstarhat= Bstar/Yss;
Nstarhat= Nstar/Yss;
CHstarhat= log(CHstar)   - log(CHstarss);
Rstarhat = 4*(log(Rstar) - log(Rss));


%%%%%%%--------------------Optimal solution---------------------

CH_til = (1-gamma)*alpha* A * LO_til^(alpha-1);

Delta_lnq2 = log(CH_til) - log(CH_til(-1));


PFstar*CFss = gamma*alpha/(1-alpha)* POstar/LO_til;

A * LO_til^alpha*O_til = PHstar_til^(-epsx) *Cstar + CH_til; 

PHstar_til = epsx/(epsx-1)* POstar/( (1-alpha)*A* LO_til^alpha );

L_til = LO_til*O_til;

%%%%%%%--------------------noFXI solution---------------------
v_noFXI = log(CH) - log(CH_til);
u_noFXI = log(CF) - log(CFss);

v_noFXI_dcp = log(CH_dcp) - log(CH_til);
u_noFXI_dcp = log(CF_dcp) - log(CFss);

Delta_lnrer = log(rer) - log(rer(-1));
  
%%%%%%%--------------------Peg solution---------------------

Delta_lnq = ( log(A) - log(A(-1)) ) - (1-alpha)*(  log(POstar) - log(POstar(-1))  ); 

Delta_lnq + v_peg - v_peg(-1) + Pi_peg = 0;

Pi_peg = (1-theta)*(1-beta*theta)/theta * v_peg + beta*Pi_peg(+1);

CHss/Yss * v_peg = log(O_peg)-log(O_til);

PFstar*CFss = gamma*alpha/(1-alpha)* POstar/LO_peg;

L_peg = LO_peg*O_peg;

v_peg = log(CH_peg) - log(CH_til);

%%%%%%%--------------------Peg with Subsidy solution---------------------

Delta_lnq + v_pegsub - v_pegsub(-1) + Pi_pegsub = 0;

Pi_pegsub = (1-theta)*(1-beta*theta)/theta * ( v_pegsub  + (1-alpha)* (log(LO_pegsub)-log(LO_til)) )+ beta*Pi_pegsub(+1);

CHss/Yss * v_pegsub -  epsx * (Yss-CHss)/Yss * ( log(PHstar_pegsub) - log(PHstar_til))= alpha*( log(LO_pegsub) - log(LO_til) ) +  log(O_pegsub)-log(O_til);

PHstar_pegsub = epsx/(epsx-1)* 1/( (1-alpha)*A* LO_pegsub^alpha );

PFstar*CFss = gamma*alpha/(1-alpha)* 1/LO_pegsub;

%L_pegsub = LO_pegsub*O_pegsub;

v_pegsub = log(CH_pegsub) - log(CH_til);
 



%----------------------------No FXI DCP Block ----------------------


rer_dcp  = gamma/(1-gamma) *CH_dcp/CF_dcp;
Delta_lnrer_dcp = log(rer_dcp) - log(rer_dcp(-1));


A * LO_dcp^alpha*O_dcp = CHstarss + Phi_dcp*CH_dcp;

Bstar_dcp/Rstar - Bstar_dcp(-1) = PHstarss*CHstarss - PFstar*CF_dcp + POstar*(Ow - O_dcp);

log(Rstar/Rss) + log(PFstar/PFstar(+1)) + log(CF_dcp/CF_dcp(+1))  = Rss *( Bstar_dcp - Nstar - Fstar_dcp  )/Yss * omega*volat_dcp*Yss; %omega*volat_dcp*Yss;

Fstar_dcp = 0;

PFstar * CF_dcp = gamma* (alpha)/(1-alpha)* POstar/(LO_dcp);
LO_dcp = L_dcp/O_dcp; 

1/R_dcp = beta* PH_dcp*CH_dcp/ ( PH_dcp(+1)*CH_dcp(+1) );

PH_dcp^(1-epsh) = (1-theta)*PH_num_dcp^(1-epsh) + theta*PH_dcp(-1)^(1-epsh);
Phi_dcp = (1-theta)* (PH_num_dcp/PH_dcp)^(-epsh) + theta*Phi_dcp(-1);
PH_num_dcp = eta*epsh/(epsh-1)* X1_dcp/X2_dcp;
X1_dcp  =PI_dcp*PH_dcp^(epsh-1) +    beta*theta*X1_dcp(+1);
X2_dcp  =       PH_dcp^(epsh-1) +    beta*theta*X2_dcp(+1);

e_dcp*POstar = (1-alpha)*PI_dcp*A *LO_dcp^alpha;

Pi_dcp   = PH_dcp/PH_dcp(-1); 
rer_dcp  = PFstar*e_dcp/PH_dcp  ;
Ld_e_dcp = 100*log(e_dcp(+1)/e_dcp); 

R_dcp/Rss      = (R_dcp(-1)/Rss)^rhor * (Pi_dcp/Piss)^(gammaPi*(1-rhor));


end;



initval;

LO = LOss;

Bstar = 0;Nstar = 0;Fstar = 0;

R = 1/beta; Rstar = 1/beta;

A = 1;
PFstar=1; POstar = 1; Phi =1; Pi  = 1 ; PH = 1; PH_num = 1; PI = 1;

X1 = PI*PH^(epsh-1) *1/(1-beta*theta);
X2 =    PH^(epsh-1) *1/(1-beta*theta);

CF     = CFss; CH = CHss;
PHstar = PHstarss; CHstar = CHstarss;
O  = Oss; L  = Lss; Y   = Yss;


e   = ess; rer = rerss;

Ld_e   = 0;d_e = 1;


CH_til = CH; LO_til = LO; O_til = O; PHstar_til = PHstar; L_til = L;

Delta_lnq = 0;v_peg = 0; Pi_peg = 0;O_peg = O;LO_peg = LO;L_peg = L;CH_peg=CH;
v_noFXI = 0; u_noFXI = 0;

v_pegsub = 0; Pi_pegsub = 0; PHstar_pegsub = PHstar  ;LO_pegsub = LO ; O_pegsub = O; CH_pegsub = CH;


rer_dcp = rer; CH_dcp = CH;CF_dcp = CF;LO_dcp = LO;
O_dcp = O;Phi_dcp = Phi;Bstar_dcp = Bstar;R_dcp = R; PH_dcp = PH;
PH_num_dcp = PH_num; X1_dcp = X1; X2_dcp = X2;
PI_dcp = PI; e_dcp =e; Ld_e_dcp = Ld_e; L_dcp =L; Pi_dcp = Pi; Fstar_dcp = 0;


ehat = 0; qhat= 0;
CHhat= 0; rerhat= 0;Rhat= 0; Lhat= 0; LOhat= 0; Pihat= 0;
CFhat= 0; Bstarhat= 0;Nstarhat= 0; CHstarhat= 0;
Rstarhat= 0; Yhat = 0;

end;


steady;
check;
%resid;
% Shocks block
shocks;
var epspos; stderr 0.158 *0.01;
var epsa  ; stderr 0.0063*0.01;
var epsn  ; stderr sigman  *0.01*1;

end;

set_dynare_seed('default');
stoch_simul(order=1, irf=60,nograph,periods=15000,irf_shocks=(epspos,epsa,epsn))
Ld_e,Ld_e_dcp,Delta_lnq,Bstar,d_e,CF,CHstar,PHstar,O,Y,PHstar  ,CH , rer ,  R , L  , Pi, e ,LO,Rstar, Bstarhat,Nstarhat,
Yhat, ehat, CHhat,rerhat,Rhat,Lhat,LOhat,Pihat,CFhat,CHstarhat,Rstarhat, Fstar
;

